const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}



//全局共有方法
const common = {
  showTipSuccess:function (msg){
    wx.showToast({ title: msg, image: "" });
  },
  showTipError:function (msg){
    wx.showToast({ title: msg, icon:"none",image: "" });
  },
  getPickerData: function (multiIndex) {
    var curDate = new Date();
    var curMinu = curDate.getMinutes()
    var pyl = 2;
    curDate.setHours(curDate.getHours() + pyl)
    var minuteIndex = Math.ceil(curMinu / 10) * 10;
    curDate.setMinutes(minuteIndex)
    var date = new Date(curDate); ////未来两个小时
    var monthDay = this.getPickdataMonthDay(date);
    var multiArray = [];
    multiArray.push(monthDay);
    var hourMinu = this.getPickdataHoursMinure(date, multiIndex);
    for (var key in hourMinu) {
      multiArray.push(hourMinu[key]);
    }
    return { multiArray: multiArray, multiIndex: multiIndex };
  },
  getPickdataHoursMinure: function (date, multiIndex) {
    var currentMinute = date.getMinutes();
    var currentHours = date.getHours();
    if (currentMinute > 50){
      currentHours++
    }
    if (multiIndex[0] != 0) {
      currentHours = 0;
      currentMinute = 0;
    }
    if (multiIndex[1] != 0){
      currentMinute = 0;
    }

    return this.loadData(currentHours, currentMinute);
  },
  checkIsTom:function(a,b){
    if(a < b){
      return true
    }
    if(a == 6 && b==0){
      return true;
    }
  },
  getPickdataMonthDay: function (date) {
    var weekday = new Array(7);
    weekday[0] = '星期天'
    weekday[1] = '星期一'
    weekday[2] = '星期二'
    weekday[3] = '星期三'
    weekday[4] = '星期四'
    weekday[5] = '星期五'
    weekday[6] = '星期六'
    weekday[7] = '星期天'
    var dateNow = new Date();
    if (this.checkIsTom(dateNow.getDay(), date.getDay()) ) {//如果当前时间已经超过2150点则直接没有今天选项
      var monthDay = ['明天（' + weekday[date.getDay()] + '）'];
    } else {
      var monthDay = ['今天（' + weekday[date.getDay()] + '）', '明天（' + weekday[date.getDay() + 1] + '）'];
    }
    for (var i = monthDay.length; i < 7; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth()+1) + "-" + date1.getDate() + '（' + weekday[(date.getDay() + i) % 7] + '）';
      monthDay.push(md);
    }
    return monthDay;
  },
  getPickerSelecterDate: function (c1, c2, c3) {
    var date = new Date();
    var c1_arr = c1.split("（");
    var monthDay = c1_arr[0];
    var c2_arr = c2.split("点");
    var hours = c2_arr[0];
    var c3_arr = c3.split("分");
    var minute = c3_arr[0];

    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (monthDay === "今天") {
    } else if (monthDay === "明天") {
      day++;
    } else {
      var day_arr = monthDay.split("-");
      month = day_arr[0];
      day = day_arr[1];
    }
    month = (month < 10 ? "0" + month : month);
    day = (day < 10 ? "0" + day : day);
    hours = (hours < 10 ? "0" + hours : hours);
    minute = (minute < 10 ? "0" + minute : minute);
    return {
      formDay: c1 + c2 + c3,
      year: year,
      month: month,
      day: day,
      hours: hours,
      minute: minute
    }
  },
  loadData: function (hours, minute) {
    var minuteIndex = Math.floor(minute / 10) * 10;
    var hours_arr = [];
    var minute_arr = [];
    if (minuteIndex == 60) {
      hours++;
      minuteIndex = 0;
    }
    // 时
    for (var i = hours%24; i < 24; i++) {
      hours_arr.push(i + "点");
    }
    // 分
    for (var i = minuteIndex%60; i < 60; i += 10) {
      minute_arr.push(i + "分");
    }
    return [hours_arr, minute_arr];
  }
}
function NetRequest({ showLoading=true,url, data, success, fail, complete, method = "POST" }) {
  var session_id = wx.getStorageSync('PHPSESSID');
  //本地取存储的sessionID    
  if (session_id != "" && session_id != null) {        
    var header = { 
      'content-type': 'application/x-www-form-urlencoded',
      'Cookie': 'PHPSESSID=' + session_id 
    }    
  } else {        
    var header = { 'content-type': 'application/x-www-form-urlencoded' }    
  }
  if (showLoading==true)
    wx.showLoading({title:"正在加载..."}) 
  wx.request({        
    url: url,       
    method: method,        
    data: data,        
    header: header,        
    success: res => {            
      if (session_id == "" || session_id == null) {                
        wx.setStorageSync('PHPSESSID', res.data.session_id) //如果本地没有就说明第一次请求 把返回的session id 存入本地     
      }            
      console.log(res);
      if (showLoading==true)
        wx.hideLoading()        
      let data = res.data            
      res['statusCode'] === 200 ? success(data) : fail(res)        
    },        
    fail: fail,        
    complete: complete    
  }) 
}


function NetRequest({ showLoading=true,url, data, success, fail, complete, method = "POST" }) {
  var session_id = wx.getStorageSync('PHPSESSID');
  //本地取存储的sessionID    
  if (session_id != "" && session_id != null) {        
    var header = { 
      'content-type': 'application/x-www-form-urlencoded',
      'Cookie': 'PHPSESSID=' + session_id 
    }    
  } else {        
    var header = { 'content-type': 'application/x-www-form-urlencoded' }    
  }
  if (showLoading==true)
    wx.showLoading({
      title:"正在加载...",
      mask:true
    }) 
  wx.request({        
    url: url,       
    method: method,        
    data: data,        
    header: header,        
    success: res => {            
      if (session_id == "" || session_id == null) {                
        wx.setStorageSync('PHPSESSID', res.data.session_id) //如果本地没有就说明第一次请求 把返回的session id 存入本地     
      }            
      console.log(res);
      if (showLoading==true)
        wx.hideLoading()        
      let data = res.data            
      res['statusCode'] === 200 ? success(data) : fail(res)        
    },        
    fail: fail,        
    complete: complete    
  }) 
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  common: common,
  NetRequest: NetRequest
}


